/*
 *    Copyright (C) 2012 Hai Bison
 *
 *    See the file LICENSE at the root directory of this project for copying
 *    permission.
 */

package group.pals.desktop.app.apksigner.ui.prefs;

import group.pals.desktop.app.apksigner.i18n.Messages;
import group.pals.desktop.app.apksigner.i18n.R;
import group.pals.desktop.app.apksigner.utils.Preferences;
import group.pals.desktop.app.apksigner.utils.Texts;
import group.pals.desktop.app.apksigner.utils.ui.Dlg;
import group.pals.desktop.app.apksigner.utils.ui.JEditorPopupMenu;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class PanelInternetConnection extends JPanel implements PreferencesFrame {

    /**
     * Auto-generated by Eclipse.
     */
    private static final long serialVersionUID = 9066934128887563275L;

    /*
     * CONTROLS
     */

    private JTextField mTextProxyHost;
    private JTextField mTextProxyPort;
    private JTextField mTextProxyUsername;
    private JPasswordField mTextProxyPassword;
    private JCheckBox mChkUseProxy;

    /**
     * Create the panel.
     */
    public PanelInternetConnection() {
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[] { 0, 0 };
        gridBagLayout.rowHeights = new int[] { 0, 0 };
        gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
        gridBagLayout.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
        setLayout(gridBagLayout);

        JPanel panel = new JPanel();
        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 0;
        add(panel, gbc_panel);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] { 0, 0, 0 };
        gbl_panel.rowHeights = new int[] { 0, 0, 0, 0 };
        gbl_panel.columnWeights = new double[] { 1.0, 1.0, Double.MIN_VALUE };
        gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
        panel.setLayout(gbl_panel);

        mChkUseProxy = new JCheckBox(Messages.getString(R.string.use_proxy));
        mChkUseProxy.setSelected(Preferences.getInstance().isUsingProxy());
        GridBagConstraints gbc_mChkUseProxy = new GridBagConstraints();
        gbc_mChkUseProxy.anchor = GridBagConstraints.WEST;
        gbc_mChkUseProxy.insets = new Insets(10, 5, 5, 5);
        gbc_mChkUseProxy.gridx = 0;
        gbc_mChkUseProxy.gridy = 0;
        panel.add(mChkUseProxy, gbc_mChkUseProxy);

        mTextProxyHost = new JTextField();
        mTextProxyHost.setBorder(new TitledBorder(null, Messages
                .getString(R.string.host), TitledBorder.LEADING,
                TitledBorder.TOP, null, null));
        mTextProxyHost.setText(Preferences.getInstance().getProxyHost());
        GridBagConstraints gbc_mTextProxyHost = new GridBagConstraints();
        gbc_mTextProxyHost.insets = new Insets(3, 3, 3, 3);
        gbc_mTextProxyHost.fill = GridBagConstraints.HORIZONTAL;
        gbc_mTextProxyHost.gridx = 0;
        gbc_mTextProxyHost.gridy = 1;
        panel.add(mTextProxyHost, gbc_mTextProxyHost);
        mTextProxyHost.setColumns(10);

        mTextProxyPort = new JTextField();
        mTextProxyPort.setBorder(new TitledBorder(null, Messages
                .getString(R.string.port), TitledBorder.LEADING,
                TitledBorder.TOP, null, null));
        mTextProxyPort.setText(Integer.toString(Preferences.getInstance()
                .getProxyPort()));
        GridBagConstraints gbc_mTextProxyPort = new GridBagConstraints();
        gbc_mTextProxyPort.insets = new Insets(3, 3, 3, 3);
        gbc_mTextProxyPort.fill = GridBagConstraints.HORIZONTAL;
        gbc_mTextProxyPort.gridx = 1;
        gbc_mTextProxyPort.gridy = 1;
        panel.add(mTextProxyPort, gbc_mTextProxyPort);
        mTextProxyPort.setColumns(10);

        mTextProxyUsername = new JTextField();
        mTextProxyUsername.setBorder(new TitledBorder(null, Messages
                .getString(R.string.username), TitledBorder.LEADING,
                TitledBorder.TOP, null, null));
        mTextProxyUsername
                .setText(Preferences.getInstance().getProxyUsername());
        GridBagConstraints gbc_mTextProxyUsername = new GridBagConstraints();
        gbc_mTextProxyUsername.insets = new Insets(3, 3, 3, 3);
        gbc_mTextProxyUsername.fill = GridBagConstraints.HORIZONTAL;
        gbc_mTextProxyUsername.gridx = 0;
        gbc_mTextProxyUsername.gridy = 2;
        panel.add(mTextProxyUsername, gbc_mTextProxyUsername);
        mTextProxyUsername.setColumns(10);

        mTextProxyPassword = new JPasswordField();
        mTextProxyPassword.setBorder(new TitledBorder(null, Messages
                .getString(R.string.password), TitledBorder.LEADING,
                TitledBorder.TOP, null, null));
        char[] proxyPassword = Preferences.getInstance().getProxyPassword();
        mTextProxyPassword.setText(proxyPassword != null ? new String(
                proxyPassword) : null);
        GridBagConstraints gbc_mTextProxyPassword = new GridBagConstraints();
        gbc_mTextProxyPassword.insets = new Insets(3, 3, 3, 3);
        gbc_mTextProxyPassword.fill = GridBagConstraints.HORIZONTAL;
        gbc_mTextProxyPassword.gridx = 1;
        gbc_mTextProxyPassword.gridy = 2;
        panel.add(mTextProxyPassword, gbc_mTextProxyPassword);

        JEditorPopupMenu.apply(this);
    }// PanelInternetConnection()

    @Override
    public boolean store() {
        Preferences.getInstance().setUsingProxy(mChkUseProxy.isSelected());
        if (mChkUseProxy.isSelected()) {
            /*
             * HOST
             */
            if (Texts.isEmpty(mTextProxyHost.getText().trim())) {
                mTextProxyHost.requestFocus();
                mTextProxyHost.selectAll();
                Dlg.showErrMsgAsync(Messages
                        .getString(R.string.msg_invalid_proxy_host));
                return false;
            }
            Preferences.getInstance().setProxyHost(
                    mTextProxyHost.getText().trim());

            /*
             * PORT
             */
            if (!mTextProxyPort.getText().matches("[0-9]+")) {
                mTextProxyPort.requestFocus();
                mTextProxyPort.selectAll();
                Dlg.showErrMsgAsync(Messages
                        .getString(R.string.msg_invalid_proxy_port));
                return false;
            }
            Preferences.getInstance().setProxyPort(
                    Integer.parseInt(mTextProxyPort.getText().trim()));

            /*
             * USERNAME
             */
            Preferences.getInstance().setProxyUsername(
                    mTextProxyUsername.getText().trim());

            /*
             * PASSWORD
             */
            Preferences.getInstance().setProxyPassword(
                    mTextProxyPassword.getPassword());
        }

        return true;
    }// store()

}
